package com.hgw.Demo06_CompositePattern.examples;

import java.util.ArrayList;

/**
 * Data time:2021/12/15 16:17
 * StudentID:2019112118
 * Author:hgw
 * Description:
 */
public class Composite extends Component{

    private ArrayList<Component> list = new ArrayList<Component>();

    @Override
    public void add(Component c) {
        list.add(c);
    }

    @Override
    public void remove(Component c) {
        list.remove(c);
    }

    @Override
    public Component getChild(int i) {
        return list.get(i);
    }

    @Override
    public void operation() {
        // 容器构建具体业务方法的实现，将递归调用成员构建的业务方法
        for (Component component : list) {
            component.operation();
        }
    }
}
